package com.hotec.PROJECT_NAME.modules.sys.controller;

import com.google.common.collect.Maps;
import com.hotec.PROJECT_NAME.component.basic.BaseController;
import com.hotec.PROJECT_NAME.component.basic.ConstantKey;
import com.hotec.PROJECT_NAME.component.basic.JsonContent;
import com.hotec.PROJECT_NAME.modules.sys.entity.SysRole;
import com.hotec.PROJECT_NAME.modules.sys.service.SysRoleService;
import com.hotec.PROJECT_NAME.utils.UtilsTreeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author llb
 */
@RestController
@RequestMapping("sys/role")
public class SysRoleController extends BaseController {
    @Autowired
    SysRoleService service;

    /**
     * 根据主键查询对象
     *
     * @param id
     * @return
     */
    @GetMapping("get")
    public JsonContent get(String id) {
        return this.success(service.get(id));
    }

//    /**
//     * 分页查询列表
//     *
//     * @param entity
//     * @param pageNum
//     * @param pageSize
//     * @return
//     */
    @PostMapping("query")
    public JsonContent query(@RequestParam(required = false, value = "pageNum", defaultValue = ConstantKey.DEFAULT_PAGE_NUM) Integer pageNum,
                             @RequestParam(required = false, value = "pageSize", defaultValue = ConstantKey.DEFAULT_PAGE_SIZE) Integer pageSize,
                             @RequestBody SysRole entity) {
        LinkedHashMap<String, Object> param = Maps.newLinkedHashMap();

        return this.success(service.queryByPage(param,pageNum, pageSize, ConstantKey.DEFAULT_ORDER_BY));
    }


    /**
     * 查询所有列表，角色不允许分页查询，不允许条件查询
     *
     * @return
     */
    @PostMapping("queryAll")
    public JsonContent queryAll() {
        return this.success(service.queryAll());
    }

    /**
     * 查询所有列表，角色不允许分页查询，不允许条件查询
     *
     * @return
     */
    @GetMapping("queryAllTree")
    public JsonContent queryAllTree() {
        //如果是超管设置,只能添加客服账号
        if (true) {
            LinkedHashMap map = Maps.newLinkedHashMap();
            SysRole role = service.getOne(new SysRole().setCode("cs-manager"));
            map.put("parentPath:LIKE", role.getParentPath());
            return this.success(new UtilsTreeBuilder(SysRole.class).listToTree(service.queryAll(map)));
        }
        return this.success(service.queryAll());
    }

    /**
     * 根据对象列表逻辑删除
     *
     * @param entityList
     * @return
     */
    @PostMapping("delete")
    public JsonContent delete(@RequestBody List<SysRole> entityList) {
        service.deleteById(entityList);
        return this.success().setMessage("删除成功！");
    }

    /**
     * 物理删除
     *
     * @return
     */
    @PostMapping("drop")
    public JsonContent drop() {
        return this.success();
    }

    /**
     * 保存更新
     *
     * @return
     */
    @PostMapping("saveAndBinding")
    public JsonContent save(@Valid @RequestBody SysRole entity) {
        service.saveAndBinding(entity);
        return this.success().setMessage("操作成功！");
    }


    /**
     * 根据对象条件，查询单条数据
     *
     * @return
     */
    @PostMapping("getOne")
    public JsonContent getOne(@RequestBody SysRole entity) {
        return this.success(service.getOne(entity));
    }

}
